package com.welsy.stats.command
{
	import com.welsy.stats.model.StatsModelLocator;
	import com.welsy.stats.vo.IterClassVO;
	import com.adobe.cairngorm.commands.ICommand;
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.control.CairngormEvent;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import com.welsy.util.NamespaceLocator;
	import mx.rpc.events.ResultEvent;
	import com.welsy.stats.vo.IterElementStatsVO;
	import com.welsy.stats.business.GetTestFromCourseDelegate;
	import com.welsy.stats.event.GetTestFromCourseEvent;
	import com.welsy.qnames.TestDataTypes;
	import com.welsy.authoring.vo.course.TestVO;
	import com.welsy.stats.view.courseview.CourseTestView;
	
	public class GetTestFromCourseCommand implements ICommand, IResponder
	{
		[Bindable] private var smodel : StatsModelLocator = StatsModelLocator.getInstance();	
		
		public function execute(event : CairngormEvent):void {
			var delegate : GetTestFromCourseDelegate = new GetTestFromCourseDelegate(this);
			var gtfc_eve : GetTestFromCourseEvent = GetTestFromCourseEvent(event);
			
			delegate.getTestFromCourse(gtfc_eve.courseId);
		}
		
		public function result(event : Object) :void {
			//parse result
			var xres : XMLList = XMLList(ResultEvent(event).result);
			
			var tests : ArrayCollection = new ArrayCollection;
			for each(var tt : XML in xres.descendants(TestDataTypes.TAG_TESTLIST).children()) {
				//TestType
				var testView : CourseTestView = new CourseTestView;
				var testVO : TestVO = new TestVO;
				testVO.testId = tt.child(TestDataTypes.TAG_TESTID);
				testVO.title = tt.child(TestDataTypes.TAG_TITLE);
				testVO.skipType = tt.child(TestDataTypes.TAG_QUESTIONSKIPTYPE);
				testVO.questionWeightType = tt.child(TestDataTypes.TAG_QUESTIONWEIGHTTYPE);
				testVO.passingCondition = tt.child(TestDataTypes.TAG_PASSINGCONDITION);
				testVO.displayCorrected = tt.child(TestDataTypes.TAG_DISPLAYCORRECTED);
				testVO.displayFeedback = tt.child(TestDataTypes.TAG_DISPLAYFEEDBACK);
				testVO.displayCorrectAnswere = tt.child(TestDataTypes.TAG_DISPLAYCORRECTANSWER);
				testVO.traceConsultationTime = tt.child(TestDataTypes.TAG_TRACECONSULTATIONTIME);
				testVO.consultationTimeLimit = tt.child(TestDataTypes.TAG_CONSULTATIONTIMELIMIT);
				testView.testVO = testVO;
				
				tests.addItem(testView);
			}
			
			smodel.coursesClassTestsB = tests;
		}
		
		public function fault( event : Object ):void {
			Alert.show("GetClassroomTestStatsCommand.fault!");
		}
		
	}
}